﻿#include "graphicsscene.h"
#include <QGraphicsView>
#include <QDebug>

enum SelectMode
{
    none,
    netSelect,
    move, //移动
    size, //改变大小
    rotate //反转
};

SelectMode selectMode = none;

SizeHandleRect::Direction nDragHandle = SizeHandleRect::None;

QPointF diffPoit = QPointF();

GraphicsScene::GraphicsScene()
{

}

void GraphicsScene::creatRect(int aleft,int atop,int awidth,int aheight)
{
    if(m_RectItem){
        return;
    }
    this->clearSelection();
    m_RectItem = new GraphicsRectItem(QRect(aleft, atop, awidth, aheight), NULL);
    this->addItem(m_RectItem);
    m_RectItem->setSelected(true);
    this->update();
}

void GraphicsScene::SetBackGroundImage(QPixmap pix, int width, int height)
{
    m_pix = pix;
    viewWidth = width;
    viewHeight = height;

    resetBackGroundIamge();
}

void GraphicsScene::resetBackGroundIamge()
{
    this->clear();
    m_RectItem = nullptr;
    //---------------------计算scale，等比例缩放-------------------------
    m_scale = 0.0;
    if((double)viewWidth/viewHeight > (double)m_pix.width()/m_pix.height()){
        m_scale = (double)viewHeight/m_pix.height();
    } else{
        m_scale = (double)viewWidth/m_pix.width();
    }

    QGraphicsPixmapItem* _backGroundItem;

    if(m_tailorRect.width() != 0)//裁剪模式下需要进行进一步的缩放计算
    {
        QPixmap tailorPix = m_pix.copy((double)m_tailorRect.topLeft().x()/m_scale,
                           (double)m_tailorRect.topLeft().y()/m_scale,
                           (double)m_tailorRect.width()/m_scale,
                           (double)m_tailorRect.height()/m_scale);
        if((double)viewWidth/viewHeight > (double)tailorPix.width()/tailorPix.height()){
            m_scale = (double)viewHeight/tailorPix.height();
        } else{
            m_scale = (double)viewWidth/tailorPix.width();
        }
        _backGroundItem = this->addPixmap(tailorPix);
    }
    else//不在裁剪模式下
    {
        _backGroundItem = this->addPixmap(m_pix);
    }

    _backGroundItem->setScale(m_scale);
}

void GraphicsScene::setTailorImage(QRect rect)
{
    if(m_tailorRect.width() == 0)
    {
            m_tailorRect = rect;
    }
    resetBackGroundIamge();
}

void GraphicsScene::cancelTailorImage()
{
    m_tailorRect = QRect(0,0,0,0);
    if(m_RectItem)
    {
        delete  m_RectItem;
        m_RectItem = nullptr;
    }
    resetBackGroundIamge();
}

void GraphicsScene::setCursor(const QCursor &cursor)
{
    QList<QGraphicsView*> views  = this->views();
    if ( views.count() > 0 ){
        QGraphicsView * view = views.first();
        view->setCursor(cursor);
    }
}

void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    PrintLog;
    QList<QGraphicsItem *> items = this->selectedItems();
    GraphicsRectItem *item = 0;
    PrintLog<<"before sub Item,items.count:"<<items.count();
    if ( items.count() == 0 )//之前选中了背景
    {
        if(event->button() == Qt::LeftButton)//鼠标左键
        {
            if(!m_RectItem and m_tailorRect.width()==0)//且裁剪窗口没有创建
            {//创建裁剪窗口
                int x = event->scenePos().x();
                int y = event->scenePos().y();
                int width = 100;
                int height = 100;
                if((x+width)>=this->width())
                {
                    width = this->width() - x;
                }
                if((y+height)>=this->height())
                {
                    height = this->height() - y;
                }
                creatRect(x,y,width,height);
            }
        }
    }
    if ( items.count() == 1 )//之前选中了裁剪框
    {
        if(event->button() == Qt::LeftButton)//鼠标左键
        {
            item = qgraphicsitem_cast<GraphicsRectItem*>(items.first());

            nDragHandle = item->hitTest(event->scenePos());
            if ( nDragHandle !=SizeHandleRect::None)//判断鼠标点击的位置
            {//点击绿点
                selectMode = size;
            }
            else
            {//点击框内
                diffPoit = event->scenePos() - QPointF(item->rect().x(),item->rect().y());
                selectMode =  move;
            }
        }
    }
    if(selectMode == move || selectMode == none)
    {
        QGraphicsScene::mousePressEvent(event);
    }
    items = this->selectedItems();
    PrintLog<<"after sub Item,items.count:"<<items.count();
    if ( items.count() == 0 )//本次点击了背景
    {
        if(event->button() == Qt::RightButton)//鼠标右键
        {//退出裁剪模式
            cancelTailorImage();
        }
    }
    if ( items.count() == 1 )//本次点击了裁剪矩形
    {
        if(event->button() == Qt::RightButton)//鼠标右键
        {//裁剪
            QRect rect = this->m_RectItem->rect().toRect();
            this->setTailorImage(rect);
        }
    }
    PrintLog;
}

void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QList<QGraphicsItem *> items = this->selectedItems();
    if(items.count() == 1)
    {
        GraphicsRectItem *item = qgraphicsitem_cast<GraphicsRectItem*>(items.first());
        if ( nDragHandle != SizeHandleRect::None && selectMode == size )
        {
            item->resizeTo(nDragHandle,event->scenePos());
        }
        else if(nDragHandle == SizeHandleRect::None && selectMode == none )
        {
            SizeHandleRect::Direction handle = item->hitTest(event->scenePos());
            if ( handle != SizeHandleRect::None)
            {
                setCursor(item->getCursor(handle));
            }
            else
            {
                setCursor(Qt::ArrowCursor);
            }
        }
        else if(nDragHandle == SizeHandleRect::None && selectMode == move )
        {
            item->move(event->scenePos() - diffPoit);
        }
    }

    this->update();
}

void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(Qt::ArrowCursor);
    selectMode = none;
    nDragHandle = SizeHandleRect::None;
    diffPoit = QPointF();
    QGraphicsScene::mouseReleaseEvent(event);
}
